/*
* @Author: Yejing
* @Date: 2023-06-10 10:39:30
* @LastEditors: Yejing
* @LastEditTime: 2023-06-25 13:47:04
* @Description: 重构搜索组件
*/
<template>
	<view class="c-navigation-bar" :style="{'position':(isFixed?'fixed':''),background:navbarBgColor}">
		<c-status-bar />
		<view class="navbar">
			<view class="left" v-if='showBack'>
				<view class="left-icon-block" :style="{'border-color':(leftIcon==''?'':leftIconBorder)}"
					@click="backClick">
					<u-icon v-if="!$slots['left-icon']" :name="leftIcon" :color="leftIconColor" :size="leftIconSize">
					</u-icon>
					<slot name="left-icon"></slot>
				</view>
			</view>
			<view class="center">
				<slot name="center-info"></slot>
				<text v-show="title" :style="{'color': titleColor}">{{ title }}</text>
				<view v-show="!title && isSearch" class="search"
					:style="{'background':searchBgcolor,'border-color':searchBorder}"> 
					<u-input :maxlength="input.maxlength" @click="inputClick" @input="inputEvent" :focus="focus"
						:disabled='input.disabled' :type="input.type" v-model="input.value" :clearable="false"
						:placeholder="input.placeholder || $t('home.placeholder')">
					</u-input>
					<view class="input-search-button">
						<u-icon name="search" size="40" color="#fff" @click="searchEvent"></u-icon>
					</view>
				</view>
			</view>
			<view class="right" v-show="title || isRightIcon">
				<slot name="rightIcon"></slot>
			</view>
		</view>
	</view>
</template>

<script>
	import color from "uview-ui/libs/function/color";

	/**
	 * c-navigation-bar 自定义导航栏
	 * @description 搜索组件，集成了本项目常见导航所需功能。
	 * @property {Boolean} isFixed 是否固定在顶部（默认true）
	 * @property {String} navbarBgColor 导航背景颜色（默认#fff）
	 */
	export default {
		name: 'c-navigation-bar',
		computed: {
			color() {
				return color
			}
		},
		props: {
			//是否聚焦
			focus:{
				type: Boolean,
				default: false
			},
			isFixed: {
				type: Boolean,
				default: false
			},
			showBack: {
				type: Boolean,
				default: true
			},
			//导航栏颜色
			navbarBgColor: {
				type: String,
				default: '#fff'
			},
			//左边按钮边框颜色
			leftIconBorder: {
				type: String,
				default: '#E8E6EA'
			},
			//左边图标
			leftIcon: {
				type: String,
				default: 'arrow-left'
			},
			//左边图标颜色
			leftIconColor: {
				type: String,
				default: '#1B1D29'
			},
			//左边图标大小
			leftIconSize: {
				type: Number,
				default: 30
			},
			//标题
			title: {
				type: String,
				default: ''
			},
			//标题颜色
			titleColor: {
				type: String,
				default: '#000000'
			},
			//搜索框颜色
			searchBgcolor: {
				type: String,
				default: '#F6F7FB'
			},
			//搜索框边框颜色
			searchBorder: {
				type: String,
				default: ''
			},
			//input内容
			input: {
				type: Object,
				default: () => ({
					value: '',
					disabled: false,
					type: 'text',
					maxlength: 10,
					placeholder: '',
				})
			},
			//是否开启搜索框
			isSearch: {
				type: Boolean,
				default: false
			},
			//是否开启右边图标
			isRightIcon: {
				type: Boolean,
				default: false
			}

		},
	
		data() {
			return {};
		},
		methods: {
			//点击input框
			inputClick() {
				this.$emit('inputClick')
			},
			//输入框内容发生变化触发此事件
			inputEvent() {
				this.$emit('inputEvent', this.input.value)
				// this.inputValueChange(this.input.value)

			},
			//搜索
			searchEvent() {
				this.$emit('searchEvent', this.input.value)
			},
			//返回
			backClick() {
				if (this.$listeners['backEvent']) {
					this.$emit('backEvent')
				} else {
					this.publicJS.backOption()
				}
			},
			//节流
			inputValueChange(val) {
				//输入搜索节流函数
				//val输入的字符串
				setTimeout(() => {
					if (val == this.input.value) {
						//停止输入2秒后执行搜索
						console.log(val)
						this.postData(val); // 调用写好的方法

					}
				}, 2000)
			},
			postData(val) {
				this.$emit('inputEvent', val)
			},
		}
	}
</script>

<style lang="scss" scoped>
	$white: #E8E6EA;

	.c-navigation-bar {
		padding: 20rpx 32rpx;
		top: 0;
		width: 100%;
		    z-index: 2;
		position: relative;
		.navbar {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.left {
				.left-icon-block {
					width: 100rpx;
					height: 100rpx;
					border-radius: 30rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					border: 2rpx solid transparent;
				}
			}

			.center {
				width: calc(100% - 130rpx);
				text-align: center;

				/deep/ uni-text {
					span {
						font-size: 32rpx;
						font-weight: 500;
						letter-spacing: 2rpx;
					}
				}

				.search {
					height: 110rpx;
					border-radius: 24rpx;
					backdrop-filter: blur(10px);
					display: flex;
					align-items: center;
					padding: 0 18rpx;
					border: 2rpx solid transparent;

					.input-search-button {
						width: 76rpx;
						height: 76rpx;
						background: #2934D0;
						border-radius: 20rpx;
						text-align: center;
						line-height: 85rpx;
					}
				}

			}

			.right {
				width: 100rpx;
				height: 100rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}
</style>
